package javafx_20092010_reeks2.michaelpontus;


import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.control.Button;
import javafx.scene.control.TextBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.CheckBox;
import javafx_20092010_reeks2.espoirgahungere.UploadCast;
import java.io.File;
import java.lang.Exception;

def WIDTH = 400;
def HEIGHT = 160;
def UPLOAD_URL = "http://localhost:8080/FileUpload/FileUpload2";

var fileToUpload:File;
var fileNameToUpload:String;
var progressUpload:Float;
var bytesToUpload:Float;

var uploadRequest:HTTPRequest;


var radioButton1IsChecked = bind radioButton1.selected;
var radioButton1 = RadioButton{
    text: "behoud formaat"

    onMouseClicked: function( e: MouseEvent ):Void {
       textFieldBreedte.editable = false;
       textFieldHoogte.editable = false;
    }
};
var radioButton2IsChecked = bind radioButton2.selected;
var radioButton2 = RadioButton{
    text: "andere formaat"
    onMouseClicked: function( e: MouseEvent ):Void {
       textFieldBreedte.editable = true;
       textFieldHoogte.editable = true;
       textFieldHoogte.text = "";
       textFieldBreedte.text = "";
    }
};

var breedte:String = bind textFieldBreedte.text;
var textFieldBreedte = TextBox {
    columns: 10
    text: "breedte"
    editable: false
};

var hoogte:String = bind textFieldHoogte.text;
var textFieldHoogte = TextBox {
    columns: 10
    text: "hoogte"
    editable: false
    onMouseClicked: function( e: MouseEvent ):Void {
    }
};

var bestand = TextBox {
    columns: 28
    text: "bestand"
    editable: true
    layoutX : 10
    layoutY : 10
}

var bestandNaam:File;

var buttonBrowse = Button {
    text: "browse"
    layoutX : 265
    layoutY : 10
    action: function() {
        var x:UploadCast = new UploadCast();
        x.createCastBrowser();
        bestandNaam = x.browseUpload();
        bestand.text = bestandNaam.getAbsolutePath();
        print(bestandNaam);
    }
}
var bselectTags = Button {
    text: "select tags"
    layoutX : 255
    layoutY : 165
    action: function() {
        print("hello");
    }
}

var checkBoxPublicIsSelected:Boolean = bind checkBoxPublic.selected;
var checkBoxPublic = RadioButton {
     text: "voor iedereen"
     onMouseClicked: function( e: MouseEvent ):Void {
           tags.editable = false;
     }
}

var tagsIsSelected:Boolean = bind selectTags.selected;
var selectTags = RadioButton {
    text: "select tags"
    onMouseClicked: function( e: MouseEvent ):Void {
        tags.editable = true;
        tags.text = "";
    }
}

var tagsSelected:String = bind tags.text;
var tags = TextBox {
    columns: 15
    text: "tags"
    editable: false
    layoutX : 110
    layoutY : 165
}

var checkBoxPassWordisChecked:Boolean = bind checkBoxPassWord.selected;
var checkBoxPassWord = CheckBox {
    text: "password"
    layoutX : 10
    layoutY : 210
    selected: false
    onMouseClicked: function( e: MouseEvent ):Void {
        jtfPassWord.editable = checkBoxPassWordisChecked;
        if(checkBoxPassWordisChecked) {
            jtfPassWord.text = "";
        } else {
           jtfPassWord.text = "password";
        }

    }
}

var jtfPassWordIsChecked:String = bind jtfPassWord.text;
var jtfPassWord = TextBox {
    text: "password"
    layoutX : 150
    layoutY : 207
    editable: false
}


var checkBoxUniqueLinkIsChecked:Boolean = bind checkBoxUniqueLink.selected;
var checkBoxUniqueLink = CheckBox {
    text: "unique link"
    layoutX : 10
    layoutY : 240

}
var buttonUpload = Button {
    text: "upload"
    layoutX : 265
    layoutY : 235
    action: function() {
        println(bestandNaam);
        start();
    }
    /*action: function() {
        println(bestandNaam);
        println(breedte);
        println(hoogte);
        println("behoud formaat {radioButton1IsChecked}");
        println("andere formaat {radioButton2IsChecked}");
        println("tags is selected {tagsIsSelected}");
        println("tags selected {tagsSelected}");
        println("public is selected {checkBoxPublicIsSelected}");
        println("pasword {checkBoxPassWordisChecked}");
        println("pasword : {jtfPassWordIsChecked}");
        println("unique link {checkBoxUniqueLinkIsChecked}");
    }*/
}

function start() {
    progressUpload = 0;
    bytesToUpload = 0;
    uploadRequest = HTTPRequest {
        location: UPLOAD_URL
        file :  bestandNaam
        onToWrite: function( size:Long ) {
            bytesToUpload = size;
        }
        onWritten: function( size:Long ) {
            progressUpload = 1.0 * size / bytesToUpload;
        }
        onDone: stop
        onException: function( ex:Exception ) {
            println(ex);
            stop();
        }
    }
    uploadRequest.start();
}

function stop() {
    progressUpload = 0;
    bytesToUpload = 0;
    if ( uploadRequest != null ) {
        uploadRequest.stop();
        uploadRequest = null;
    }
}

Stage {
    title: "Swing Components"
    width: 350
    height: 300
    resizable: false
    visible: true
    scene:bind
    Scene{
        content : [bestand,buttonBrowse,buttonUpload,tags,bselectTags,
                   checkBoxPassWord,jtfPassWord,checkBoxUniqueLink,
                  VBox{
                    content:[radioButton1,radioButton2]
                    layoutX:10
                    layoutY:70
                  },
                  VBox{
                    content:[textFieldBreedte,textFieldHoogte]
                    layoutX:125
                    layoutY:90
                  },
                  VBox{
                    content:[checkBoxPublic,selectTags]
                    layoutX:10
                    layoutY:150
                  }
                  ]
    }
}